package com.wdl.sm.webui.yang;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.nokia.taiji.yang.schema.Extensible;
import com.nokia.taiji.yang.schema.Extension;
import com.nokia.taiji.yang.schema.SchemaNode;
import com.nokia.taiji.yang.schema.YANG_Container;
import com.nokia.taiji.yang.schema.YANG_Leaf;
import com.nokia.taiji.yang.schema.YANG_List;
import com.nokia.taiji.yang.schema.YangType;
import com.nokia.taiji.yang.schema.YangType.TypeInteger;
import com.nokia.taiji.yang.schema.YangType.TypeString;
import com.nokia.taiji.yang.schema.YangTypeDef;
import com.wdl.sm.webui.yang.node.*;
import com.wdl.sm.webui.yang.type.*;

/**
 * All YANG Extension Constants
 */
public class YANGExt {
    private static final Logger LOG = LoggerFactory.getLogger("YANGExt");

    // For xPATH
    public static final String FREE_ID_EDITABLE = "free-id-editable";

    public static synchronized void cleanUp() {
    }

    public static String getExtension(Extensible ext, String name) {
        if (ext == null || name.isEmpty()) {
            return "";
        }

        Extension extension = ext.getExtension(name);
        return extension == null ? "" : extension.getArgument();
    }

    public static NormalizedValue buildWebuiYangValue(YangType type) {
        if (type instanceof TypeInteger) {
            return new NumericValue((TypeInteger) type);
        }

        if (type instanceof TypeString) {
            return new StringValue((TypeString) type);
        }

        if (type instanceof YangTypeDef) {
            LOG.error("Type is from typeDef, need to get its basic type and restrictions, not available yet!!!");
        }

        LOG.error("Failed to initialate type: " + type + ", need to add support for it!!!!");

        return null;
    }

    public static Object buildWebuiYangNode(SchemaNode node) {
        if (node instanceof YANG_Container) {
            return new ContainerNode((YANG_Container) node);
        }
        
        if (node instanceof YANG_Leaf) {
            if (isQualified((YANG_Leaf) node)) {
                return new LeafNode((YANG_Leaf) node);
            } else {
                return null;
            }
        }
        
        if (node instanceof YANG_List) {
            return new ListNode((YANG_List) node);
        }

        LOG.error("Failed to initialate the node of " + node.getClass().getName() + ", need to add support for it!!!!");
        return null;
    }

    private static boolean isQualified(YANG_Leaf node) {
        return true;
    }
}
